"use strict";
exports.__esModule = true;
var Crawler = require("crawler");
var url = require('url');
var fs = require('fs');
var util_1 = require("./libs/util");
var c1Callback = function (err, res) {
    var $ = res.$;
    var $tr = $('#content table.ruler tbody tr');
    var host = "http://www.tournamentsoftware.com/sport/";
    var parsed = util_1.slice.call($tr).map(function (el) {
        var $el = $(el);
        return {
            url: host + $el.find('.drawname a').attr('href'),
            discipline: util_1.toDiscipline($el.find('.drawname a').text()),
            size: $el.find('.drawname').next().text(),
            type: $el.find('.drawname').next().next().text(),
            qualification: util_1.resolveBoolean($el.find('.drawname').next().next().next().text())
        };
    });
    return parsed.map(function (el) { return el.url; });
};
var c2Callback = function (err, res) {
    if (err) {
        return console.log(err);
    }
    var $ = res.$, body = res.body;
    var title = $('.title h3').text();
    console.log(title);
    fs.writeFile("html/" + title + ".html", body, function () { });
};
var c = new Crawler({
    rateLimit: 2000,
    maxConnections: 1
});
c.queue({
    uri: 'http://www.tournamentsoftware.com/sport/draws.aspx?id=BB4B9094-EBD8-4769-845C-DBA94B0B790B',
    callback: function (err, res, done) {
        var urls = c1Callback(err, res);
        var c1 = new Crawler({
            rateLimit: 2000,
            maxConnections: 1,
            callback: function (err, res, done) {
                c2Callback.apply(this, arguments);
                done();
            }
        });
        c1.queue(urls);
        done();
    }
});
